package framework;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import org.apache.log4j.Category;

import com.iut.reservfilm.application.ReservfilmTechniqueException;

import framework.action.*;


public class Heart extends HttpServlet {

  private static Category log = Category.getInstance(Heart.class.getName());

  public void init() throws ServletException {
    ActionFrameWork.initApplication( getServletContext() );
    log.info("*** INITIALISATION SERVLET OK ***");
  }
  


  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("doGet " + request.getSession().getId() + " - action = " + request.getParameter("action"));
    String uri = getUri(request, response);
	//sendRedirect( uri, request, response );
	forward( uri, request, response );
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("doPost " + request.getSession().getId() + " - action = " + request.getParameter("action"));
    String uri = getUri(request, response);
	//sendRedirect( uri, request, response );
	forward( uri, request, response );
  }

  private void forward(String uri, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("forward " + request.getSession().getId() + " - uri=" + uri);
    getServletContext().getRequestDispatcher( uri ).forward(request, response);
  }

  private void include(String uri, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("include " + request.getSession().getId() + " - uri=" + uri);
    getServletContext().getRequestDispatcher( uri ).include(request, response);
  }

  private void sendRedirect(String uri, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("sendRedirect " + request.getSession().getId() + " - uri=" + uri);
    response.sendRedirect( uri );
  }

  private boolean sessionValide (HttpServletRequest request) {
    return (
             ( request.getSession().getAttribute("user")!=null ) ||
             ( request.getParameter("action")!=null && request.getParameter("action").equals("start") ) ||
             ( request.getParameter("action")!=null && request.getParameter("action").equals("logon") )
           );
  }

  private String getUri(HttpServletRequest request, HttpServletResponse response) {
    String uri = null;
    boolean gotoapplihome = false;
    if ( sessionValide(request) ) {
      String action = request.getParameter("action");
      if ( action!=null ) {
        if ( action.equals("start") )
          uri = ActionFrameWork.forwardToConnection( request );
        else if ( action.equals("logon") )
          uri = ActionFrameWork.connection( request );
        else if ( action.equals("logout") )
          uri = ActionFrameWork.forwardToConnection( request );
		else if ( action.equals("gotoapplihome") ){
		  uri = ActionFrameWork.gotoAppliHome( request );
		  gotoapplihome = true;
		}
		/**************/
		else if( action.equals("addfilm") )
			uri = ActionFrameWork.addFilm(request);
		else if(action.equals("addempl"))
			uri = ActionFrameWork.addEmpl(request);
		else if(action.equals("saveuser"))
			uri = ActionFrameWork.saveUser(request);
		else if(action.equals("savefilm"))
        	uri = ActionFrameWork.saveFilm(request);
		else if(action.equals("reservfilm"))
			try {
				uri = ActionFrameWork.reservFilm(request);
			} catch (ReservfilmTechniqueException e) {
				uri = "/err.jsp";
				request.setAttribute("err", "impossible de reserver le film");
				e.printStackTrace();
			}
		else if(action.equals("reservfilmbycli"))
			uri = ActionFrameWork.reservFilmByCli(request);
		else if(action.equals("supprfilm"))
			uri = ActionFrameWork.supprFilm(request);
		else if(action.equals("suppruser"))
			uri = ActionFrameWork.supprUser(request);
		else if(action.equals("listclient"))
			uri = ActionFrameWork.listCli(request);
		else if(action.equals("supprreserv"))
				uri = ActionFrameWork.annuleReserv(request);
		else if(action.equals("detailuser"))
			uri = ActionFrameWork.detailUser(request);
		else if(action.equals("louereserv"))
			uri = ActionFrameWork.loueReserv(request);
		else if(action.equals("rendureserv"))
			uri = ActionFrameWork.renduReserv(request);
        
        
        /**************/
        else {
          log.error("action inconnue");
          uri = ActionFrameWork.removeSession( request );
        }
      } else {
        log.error("aucune action definie");
        uri = ActionFrameWork.removeSession( request );
      }
    } else {
      log.error("session non valide");
      uri = ActionFrameWork.removeSession( request );
    }
    if(uri == "/applihome.jsp" && !gotoapplihome){
    	uri = ActionFrameWork.gotoAppliHome(request);
    }
    return uri;
  }

}